package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.util.LocalAttrUtils;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 资源预占期
 * Created by cheng.leizhen01 on 2021/03/24 12:14
 */
@Service
public class ResourcePreoccupationTimeHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    private IQuerySceneCommonService querySceneCommonService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String custId = KiteWebContext.getContext().getCust().getCustId();
        String serviceOfferId = Optional.ofNullable(kiteSceneInst).map(KiteSceneInst::getServiceOfferId).orElse("");
        KiteInst kiteInst = querySceneInstService.getKiteInst(custId, kiteSceneInst.getRootInstType(), kiteSceneInst.getRootInstId());
        KiteProdInst prodInst = querySceneInstService.listProdInstsFromOfferInst(kiteSceneInst.getCustId(), kiteSceneInst.getRootInstId()).stream()
            .filter(inst -> !KiteStringUtils.equalsAny(inst.getActionType().name(), ActionType.X.name(), ActionType.D.name()))
            .findFirst().orElse(null);
        if(KiteStringUtils.equals(serviceOfferId, ServiceOfferConsts.SERVICE_OFFER_ORDER)) {
            List<DcPublic> list = DcPublicCache.get("20210719001");
            // 是否五大双线落地方产品订购，不显示资源预占期
            boolean flag = (list != null) && list.stream().anyMatch(dcPublic -> KiteStringUtils.isEqual(kiteInst.getSpecId(), dcPublic.getCodeb()));
            if (flag) {
                commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);

                KiteAttr kiteAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10013");
                kiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                operateSceneInstService.setCommonAttr(sceneInstId, kiteAttr);
                setCommonAttr(commonAttr);
                return;
            }
        }
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        //公共模块处理时nodecode已删除 添加判断相同值不再校验
        KiteAttr kiteAttr = querySceneCommonService.getCommonAttr(sceneInstId, fieldName).getResultObject();
        if (Objects.nonNull(kiteAttr) && KiteStringUtils.isNotEmpty(value) && value.equals(kiteAttr.getValue())) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        //重提初始化不校验
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams) && extParams.containsKey("nodeCode")) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        // 变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(sceneInst.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.BUSI_CHANGE_SCENE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(sceneInstId, "initSource"))) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        if (KiteStringUtils.isNotEmpty(value)) {
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate = sdf.format(d);
            //资源预占期不能早于当前时间
            if (Objects.equals(-1, KiteDateUtils.compareDate(value, nowDate, KiteDateUtils.DATE_FORMAT))){
                BssException.throwOut("", "",  "资源预占期不能早于当前时间");
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }

}
